package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Goods;
import com.example.service.GoodsService;
import com.example.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "物品管理")
@CrossOrigin
@RequestMapping("/goods")
@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @ApiOperation(value = "查询单个物品")
    @GetMapping("getOneGoods/{goodsId}")
    public R getOne(@PathVariable String goodsId) {
        Goods goods = goodsService.getById(goodsId);
        return R.ok().data("goods", goods);
    }

    @ApiOperation(value = "分页查询物品")
    @GetMapping("getGoods/{current}/{limit}")
    public R getGoods(
            @ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable long limit
    ) {

        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        //实现分页
        Page<Goods> goodsPage = new Page<>(current, limit);
        IPage<Goods> page = goodsService.page(goodsPage, wrapper);

        //获取分页后数据
        long total = page.getTotal();
        List<Goods> list = page.getRecords();

        return R.ok().data("total", total).data("list", list);
    }

    @ApiOperation(value = "分页带条件查询物品")
    @PostMapping("getGoods/{current}/{limit}")
    public R searchGoods(
            @ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable long limit,
            @RequestBody(required = false) Goods goods
    ) {

        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(goods)) {
            if (!StringUtils.isEmpty(goods.getUserId())) {
                wrapper.eq("user_id", goods.getUserId());
            }
            if (!StringUtils.isEmpty(goods.getIsExhibit())) {
                wrapper.eq("is_exhibit", goods.getIsExhibit());
            }
        }
        wrapper.orderByDesc("gmt_create");
        //实现分页
        Page<Goods> goodsPage = new Page<>(current, limit);
        IPage<Goods> page = goodsService.page(goodsPage, wrapper);

        //获取分页后数据
        long total = page.getTotal();
        List<Goods> list = page.getRecords();

        return R.ok().data("total", total).data("list", list);
    }

    @ApiOperation(value = "逻辑删除物品")
    @DeleteMapping("deleteGoods/{id}")
    public R deleteGoods(@PathVariable String id) {
        boolean b = goodsService.removeById(id);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "修改物品")
    @PostMapping("updateGoods")
    public R updateOrder(@RequestBody Goods goods) {
        boolean b = goodsService.updateById(goods);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "批量增加用户")
    @PostMapping("addGoods")
    public R addUser(@RequestBody List<Goods> goods) {
        boolean b = goodsService.saveBatch(goods);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}
